#ifndef _MY_UTIL_
#define _MY_UTIL_
#include <jsoncpp/json/json.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include "httplib.h"
class JsonUtil {
   public:
    static bool Serialize(const Json::Value& root, std::string* str) {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(root, &ss);
        if (ret != 0) {
            std::cerr << "serialize fail" << std::endl;
            return false;
        }
        *str = ss.str();
        return true;
    }
    static bool UnSerialize(const std::string& str, Json::Value* root) {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        if (cr->parse(str.c_str(), str.c_str() + str.size(), root, &err) == false) {
            std::cerr << err << std::endl;
            return false;
        }
        return true;
    }
};
class FileUtil {
   private:
    std::string name_;

   public:
    FileUtil(const std::string& name)
        : name_(name) {}
    // 判断文件是否存在
    bool IsExist() {
        if (access(name_.c_str(), F_OK) != 0) {
            std::cerr << name_ << "不存在" << std::endl;
            return false;
        }
        return true;
    }
    // 获取文件大小
    size_t Size() {
        if (this->IsExist() == false) {
            return 0;
        }
        struct stat st;
        int ret = stat(name_.c_str(), &st);
        if (ret != 0) {
            return 0;
        }
        return st.st_size;
    }
    // 读取文件数据
    bool GetContent(std::string* body) {
        std::ifstream ifs;
        ifs.open(name_, std::ios::binary);
        if (ifs.is_open() == false) {
            std::cerr << name_ << ":open file fail" << std::endl;
            return false;
        }
        size_t len = this->Size();
        body->resize(len);
        ifs.read(const_cast<char*>(body->c_str()), len);
        if (ifs.good() == false) {
            std::cerr << name_ << ":read file fail" << std::endl;
            ifs.close();
            return false;
        }
        return true;
    }
    // 将数据写入文件
    bool SetContent(const std::string& body) {
        std::ofstream ofs;
        ofs.open(name_, std::ios::binary);
        if (ofs.is_open() == false) {
            std::cerr << name_ << ":open file fail" << std::endl;
            return false;
        }
        ofs.write(body.c_str(), body.size());
        if (ofs.good() == false) {
            std::cerr << name_ << ":write file fail" << std::endl;
            ofs.close();
            return false;
        }
        return true;
    }
    // 创建目录
    bool CreateDirectory() {
        if (this->IsExist() == true) {
            return false;
        }
        int ret = mkdir(name_.c_str(), 0777);
        if (ret != 0) {
            return false;
        }
        return true;
    }
};

class ResponseUtil {
   public:
    static void FailWithMessage(const std::string& message, const int status, httplib::Response& res) {
        res.status = status;
        res.set_header("Content-Type", "application/json");
        Json::Value response_json;
        response_json["reason"] = message;
        response_json["reault"] = false;
        std::string serialize_json;
        JsonUtil::Serialize(response_json, &serialize_json);
        res.body = serialize_json;
    }
};

#endif